﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class sensor88_list: System.Web.UI.Page
    {
        public string typename;
        public string typename11;
        public int monitorid;
        public int year1;
        public int yue1;
        public int shi1;
        public int ri1;
        public int fen1;

        public int year2;
        public int yue2;
        public int shi2;
        public int ri2;
        public int fen2;

        public string mytime1;
        public string mytime2;
        protected void Page_Load(object sender, EventArgs e)
        {
            typename = Server.UrlDecode(Request.QueryString["type"].ToString());
            typename11 = Server.UrlEncode(Request.QueryString["type"].ToString());
            monitorid = int.Parse(Request.QueryString["areano"].ToString());


            if (!IsPostBack)
            {
                data_list();
                data_listid();
            }

            data_tux();
            mytime1 = this.txtstartingtime.Value.ToString().Trim();
            mytime2 = this.txtendingtime.Value.ToString().Trim();
            string[] two = mytime1.Split(' ');
            string[] starttime = two[0].Split('-');
            year1 = int.Parse(starttime[0].ToString());
            yue1 = int.Parse(starttime[1].ToString());
            ri1 = int.Parse(starttime[2].ToString());
            string[] startshi = two[1].Split(':');
            shi1 = int.Parse(startshi[0].ToString());
            fen1 = int.Parse(startshi[1].ToString());

            string[] twos = mytime2.Split(' ');
            string[] endtime = twos[0].Split('-');
            year2 = int.Parse(endtime[0].ToString());
            yue2 = int.Parse(endtime[1].ToString());
            ri2 = int.Parse(endtime[2].ToString());
            string[] endtimes = twos[1].Split(':');
            shi2 = int.Parse(endtimes[0].ToString());
            fen2 = int.Parse(endtimes[1].ToString());
        }

        protected void data_list()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + node_mac + "";
            DataSet ds = new BLL.usr2node().GetList(sql);
            DataTable dt = ds.Tables[0];

            if (dt.Rows.Count > 0)
            {
                ListView1.DataSource = dt;
                ListView1.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }
        public DataTable returnmonitor(object id)
        {
            string sql = " [index]=" + int.Parse(id.ToString()) + "";
            DataSet ds = new BLL.monitor().GetList(sql);
            return ds.Tables[0];
        }
        protected string data_type(object id)
        {
            string Status = "";
            DataTable dt = returnmonitor(id);
            Status = dt.Rows[0]["Province_name"].ToString() + dt.Rows[0]["m_area_City"].ToString() + dt.Rows[0]["m_area_name"].ToString();
            dt.Clear();
            dt.Dispose();
            return Server.UrlEncode(Status);
        }

        protected string data_type111(object id)
        {
            string Status = "";
            DataTable dt = returnmonitor(id);
            Status = dt.Rows[0]["Province_name"].ToString() + dt.Rows[0]["m_area_City"].ToString() + dt.Rows[0]["m_area_name"].ToString();
            dt.Clear();
            dt.Dispose();
            return Server.UrlDecode(Status);
        }

        protected string data_renyuan(object id)
        {
            string Status = "";
            DataTable dt = returnmonitor(id);
            Status = dt.Rows[0]["contact_name1"].ToString();
            dt.Clear();
            dt.Dispose();
            return Status;
        }

        protected string data_phone(object id)
        {
            string Status = "";
            DataTable dt = returnmonitor(id);
            Status = dt.Rows[0]["contact_mobile1"].ToString();
            dt.Clear();
            dt.Dispose();
            return Status;
        }

        public string tux;
        public string cheryalue3;
        protected void data_tux()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string sql = " [mac]=" + int.Parse(node_mac.ToString()) + "";
            DataSet ds = new BLL.gprsNode().GetList(sql);
            DataTable dt = ds.Tables[0];
            tux = dt.Rows[0]["tux"].ToString();
            cheryalue3 = dt.Rows[0]["charvalue3"].ToString();
            dt.Clear();
            dt.Dispose();
        }

        protected string data_typename(object id)
        {
            int typeid = int.Parse(id.ToString());
            string sql = " adminno=" + typeid + "";
            DataSet ds = new BLL.sensor_node().GetList(sql);
            return ds.Tables[0].Rows[0]["name"].ToString();
        }
        protected string data_addrname(object id)
        {
            string addrname = "";
            DataTable dt = returnmonitor(id);
            if (dt.Rows.Count > 0)
            {
                addrname = dt.Rows[0]["addr"].ToString();
            }

            return addrname;
        }

        protected string data_count(object id)
        {
            string count = "";
            if (Request.QueryString["type"].ToString() == "66")
                count = (float.Parse(id.ToString()) / 100).ToString("0.00");

            return count;
        }

        protected string data_jibie(object id)
        {
            string name = id.ToString();
            if (name == "正常")
            {
                name = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                name = "<a style=\"color:red;\">" + name + "</a>";
            }
            return name;
        }

        protected void data_listid()
        {
            int node_mac = int.Parse(Request.QueryString["mac"].ToString());
            string strwhere = " mac=" + node_mac + "";
            DataSet ds = new BLL.gprsNodeLog().GetList(10, strwhere, " [chartime] desc");

            DataTable data = ds.Tables[0];
            int time1;
            int time10;
            int year1;
            int year10;

            if (data.Rows.Count > 0)
            {
                time1 = int.Parse(data.Rows[data.Rows.Count - 1]["time"].ToString());
                time10 = int.Parse(data.Rows[0]["time"].ToString());
                year1 = int.Parse(data.Rows[data.Rows.Count - 1]["year"].ToString());
                year10 = int.Parse(data.Rows[0]["year"].ToString());
                data_datatime1(time1, year1);
                data_datatime10(time10, year10);
                if (!IsPostBack)
                {
                    data_datatime1(time1, year1);
                    data_datatime10(time10, year10);
                }
                if (data.Rows.Count > 0)
                {
                    ListView2.DataSource = data;
                    ListView2.DataBind();
                }

                data.Clear();
                data.Dispose();
            }


        }

        protected string data_Status(object id)
        {
            string Status = "";
            if (id.ToString() == "0")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常</a>";

            }
            return Status;
        }

        protected string data_jingbao(object id)
        {
            string Status = "";
            switch (int.Parse(id.ToString()))
            {
                case 0:
                    Status = "<a style=\"color:green;\">正常</a>";
                    break;
                case 1:
                    Status = "<a style=\"color:red;\">报警</a>";
                    break;
                case 2:
                    Status = "<a style=\"color:orange;\">预警</a>";
                    break;
                default:
                    Status = "<a style=\"color:red;\">设备异常</a>";
                    break;
            }
            //if (int.Parse(id.ToString()) == 0)
            //{
            //    Status = "<a style=\"color:green;\">正常</a>";
            //}
            //else
            //{
            //    Status = "<a style=\"color:red;\">异常</a>";
            //    break;
            //}
            return Status;
        }


        protected string data_wendu(object id)
        {
            string Status = "";
            if (id.ToString() == "1")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常高</a>";

            }
            return Status;
        }

        protected string data_dianchi(object id)
        {
            string Status = "";
            if (id.ToString() == "1")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常</a>";

            }
            return Status;
        }

        protected string data_dianliang(object id)
        {
            string Status = "";
            if (id.ToString() == "1")
            {
                Status = "<a style=\"color:green;\">正常</a>";
            }
            else
            {
                Status = "<a style=\"color:red;\">异常:低</a>";

            }
            return Status;
        }

        protected void data_datatime1(int timeid, int year)
        {
            int id = timeid;
            int fen = id & 0x000000ff;
           // this.TextBox4.Text = fen.ToString();
            int shi = id >> 8 & 0x000000ff;
           // this.TextBox3.Text = shi.ToString();
            int ri = id >> 16 & 0x000000ff;
           // this.TextBox2.Text = ri.ToString();
            int yue = id >> 24 & 0x000000ff;
            //this.TextBox1.Text = yue.ToString();
           // this.TextBox5.Text = year.ToString();
            string time = year.ToString() + "-" + yue.ToString() + "-" + ri.ToString() + " " + shi.ToString() + ":" + fen.ToString() + ":00";
            this.txtstartingtime.Value = time;
        }


        protected string data_zhuangt(object id)
        {
            string zhuangt = "";
            string typeid = id.ToString();
            if (typeid == "正常")
            {
                zhuangt = "<a style=\"color:green;\">正常</a>";
            }
            else if (typeid == "预警")
            {
                zhuangt = "<a style=\"color:orange;\">预警</a>";
            }
            else
            {
                zhuangt = "<a style=\"color:red;\">" + typeid + "</a>";
            }
            return zhuangt;
        }


        protected void data_datatime10(int timeid, int year)
        {
            int id = timeid;
            int fen = id & 0x000000ff;
            //this.TextBox10.Text = fen.ToString();
            int shi = id >> 8 & 0x000000ff;
            //this.TextBox9.Text = shi.ToString();
            int ri = id >> 16 & 0x000000ff;
            //this.TextBox8.Text = ri.ToString();
            int yue = id >> 24 & 0x000000ff;
            //this.TextBox7.Text = yue.ToString();
            //this.TextBox6.Text = year.ToString();
            string time = year.ToString() + "-" + yue.ToString() + "-" + ri.ToString() + " " + shi.ToString() + ":" + fen.ToString() + ":00";
            this.txtendingtime.Value = time;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["mac"].ToString());

            int shijian1 = (yue1 << 24) | (ri1 << 16) | (shi1 << 8) | fen1;

            int shijian2 = (yue2 << 24) | (ri2 << 16) | (shi2 << 8) | fen2;

            string sql = " mac=" + id + "";
            DataSet ds = new BLL.gprsNodeLog().GetList(sql);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                string sq = " mac = " + id + " and CAST(REPLACE(REPLACE (REPLACE (chartime,'年','-'),'月','-'),'日','') AS DateTime)   between " + " CAST('" + mytime1 + "' AS DateTime)" + " and CAST('" + mytime2 + "' AS DateTime) order by [index] desc";
                DataSet ds1 = new BLL.gprsNodeLog().GetList(sq);
                dt = ds1.Tables[0];
            }
            ListView2.DataSource = dt;
            ListView2.DataBind();
           // Repeater2.DataSource = dt;
            //Repeater2.DataBind();
        }
    }
}